package core.sceneManager.sceneSection.scene3DSection
{
	import core.sceneManager.sceneSection.SceneSection;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import memory.MemoryRecovery;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	/**
	 * 3d全景球
	 */ 
	public class CustomSphere extends SceneSection
	{
		private var loader:Loader;                         //用来加载全景球材质
		private const maxTries:int=3;                      //最大重试次数
		private var numTries:int=0;                        //已经重试的次数，当重试的次数等于最大重试次数的时候，就不在重试加载
		private var url:String;                            //素材的路径
		private static var cacheShpereBitmap:BitmapData;   //全景球的材质图片
		private var request:URLRequest;
		private var sphere:Sphere;                          //全景球
		private var _waitForComplete:Boolean;
		public function CustomSphere(target:IEventDispatcher=null)
		{
			super(target);
		}
		override public function excute(xml:XML, container:*):void
		{
			sphere=new Sphere(new ColorMaterial(0xffffff, 0), 5000, 30, 30);
			sphere.name="CustomSphere";
			sphere.scaleX=sphere.scaleX*-1;
			container.addChild(sphere,"CustomSphere");
			this.dataBase=xml;
			for each(var item:XML in xml.attributes())
			{
				try
				{
					var key:String=item.name();
					this[key]=item.toString();
				}catch(e:Error){}
			}
		}
		override public function start():void
		{
			if(url!=null && url!="")
			{
				loader=new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,on_bitmap_loaded);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,on_IO_error);
				if(request==null) request=new URLRequest(url);
				loader.load(request);
			}else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		//全景球素材加载完毕的时候
		private function on_bitmap_loaded(e:Event):void
		{
			MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,Event.COMPLETE,on_bitmap_loaded);
			cacheShpereBitmap = Bitmap(loader.content).bitmapData;
			var material:BitmapMaterial=new BitmapMaterial(cacheShpereBitmap, true);
            material.doubleSided = false;
            material.smooth = true;
            material.interactive = false;
			sphere.material=material;
			if(_waitForComplete)this.dispatchEvent(new Event(Event.COMPLETE));                             //全景球创建完毕
		}
		//数据加载失败的时候触发
		private function on_IO_error(e:IOErrorEvent):void
		{
			if(numTries<maxTries)
			{
				try
				{
					loader.unload();
					loader.close();
				}catch(e:Error)
				{
					
				}
				loader.load(request);
			}else
			{
				dispatchEvent(new Event(Event.COMPLETE));            //数据加载完毕就算是全景球创建完毕
				dispose();
				trace("CustomSphere放弃加载:"+this.url);
			}
		}
		override public function dispose():void
		{
			MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,Event.COMPLETE,on_bitmap_loaded);
			MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,IOErrorEvent.IO_ERROR,on_IO_error);
			MemoryRecovery.getInstance().gcObj(cacheShpereBitmap);
			cacheShpereBitmap=null;
			MemoryRecovery.getInstance().gcObj(sphere);
			sphere=null;
			type=null;
			sectionName=null;
			url=null;
			request=null;
			MemoryRecovery.getInstance().gcObj(loader);
			loader=null;
		}
		/////////////////////////////////////扩展属性，可以通过XML进行配置,暂时没有///////////////////////////////
		private function set waitForComplete(val:int):void
		{
			if(val==1)
			{
				_waitForComplete=true;
			}
		}
	}
}